Ionic অ্যাপ্লিকেশনে Local Notifications ব্যবহার করার জন্য Capacitor অথবা Cordova প্লাগইন ব্যবহার করা যেতে পারে। এখানে আমরা Capacitor Local Notifications প্লাগইন ব্যবহার করে Local Notifications সেটআপ করার প্রক্রিয়া নিয়ে আলোচনা করবো, কারণ এটি Ionic অ্যাপ্লিকেশনের সাথে আধুনিক ও অফিশিয়াল সমর্থন পায়।
১. Capacitor Local Notifications প্লাগইন ইনস্টল করা
প্রথমে, Capacitor Local Notifications প্লাগইন ইনস্টল করতে হবে। এটি ইনস্টল করতে নিচের কমান্ডটি ব্যবহার করুন:
npm install @capacitor/local-notifications
npx cap sync
এই কমান্ডটি প্লাগইন ইনস্টল করবে এবং প্রয়োজনীয় ফাইল এবং কনফিগারেশন আপনার প্রজেক্টে সিঙ্ক্রোনাইজ করবে।
২. Capacitor প্লাগইন কনফিগারেশন
Capacitor Local Notifications প্লাগইন ইনস্টল করার পর, Android এবং iOS প্ল্যাটফর্মে এটি সঠিকভাবে কাজ করার জন্য কিছু কনফিগারেশন করতে হবে।
২.১ Android কনফিগারেশন
AndroidManifext.xml ফাইলে
INTERNETএবংWAKE_LOCKপারমিশন অ্যাড করুন:<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WAKE_LOCK"/>- Notification Channel তৈরি করুন, Android 8.0 (API level 26) বা তার পরের সংস্করণে Local Notifications সঠিকভাবে কাজ করতে হলে Notification Channel প্রয়োজন।
import { LocalNotifications } from '@capacitor/local-notifications';
// Create the notification channel
const channel = await LocalNotifications.createChannel({
id: 'default',
name: 'Default notifications',
description: 'A default channel',
sound: 'beep.wav',
importance: 3,
});
২.২ iOS কনফিগারেশন
iOS এর জন্য, Background Modes সক্রিয় করতে হবে:
- Xcode এ গিয়ে আপনার প্রজেক্ট খুলুন।
- Capabilities ট্যাবে গিয়ে Background Modes সক্রিয় করুন এবং Remote notifications নির্বাচন করুন।
- App Sandbox > Push notifications চালু করুন।
৩. Local Notifications পাঠানো
এখন আপনি Local Notification পাঠাতে পারবেন। নিচে উদাহরণ দেয়া হলো কিভাবে Ionic অ্যাপে একটি Local Notification পাঠানো যায়।
৩.১ Local Notification পাঠানো
import { LocalNotifications } from '@capacitor/local-notifications';
async function scheduleNotification() {
await LocalNotifications.schedule({
notifications: [
{
title: "My First Notification",
body: "This is a test notification",
id: 1,
schedule: { at: new Date(new Date().getTime() + 1000 * 5) }, // 5 seconds later
sound: null,
attachments: null,
actionTypeId: '',
extra: null
}
]
});
}
- title: নোটিফিকেশনের শিরোনাম।
- body: নোটিফিকেশনের বডি বা বার্তা।
- id: প্রতিটি নোটিফিকেশনের একটি ইউনিক আইডি।
- schedule: কখন নোটিফিকেশনটি পাঠানো হবে। উদাহরণস্বরূপ, এখানে এটি ৫ সেকেন্ড পর পাঠানো হবে।
- sound: আপনি যদি একটি সাউন্ড দিতে চান তবে তা এখানে নির্ধারণ করতে হবে।
- attachments: ইমেজ বা অন্যান্য অ্যাটাচমেন্ট ব্যবহার করতে পারেন।
৩.২ নোটিফিকেশন হ্যান্ডলিং
একটি নোটিফিকেশন ক্লিক করার পর অ্যাপটি কি করবে তা নির্ধারণ করা যেতে পারে। এটি addListener ব্যবহার করে হ্যান্ডল করা যায়:
import { LocalNotifications } from '@capacitor/local-notifications';
LocalNotifications.addListener('localNotificationActionPerformed', (notification) => {
console.log('Notification clicked:', notification);
// Handle the notification action (e.g., navigate to a specific page)
});
এখানে, যখন কোনো নোটিফিকেশন ক্লিক করা হয় তখন তা কনসোলে লগ হবে এবং আপনি আপনার অ্যাপের নির্দিষ্ট পেজে নেভিগেট করার মতো কাজ করতে পারেন।
৪. Notifying the User with Ongoing Notifications
আপনি যদি ongoing notifications পাঠাতে চান, যেগুলি ইউজারের অ্যাকশন না হওয়া পর্যন্ত চলে, তখন সেগুলোর জন্য ongoing ফ্ল্যাগ ব্যবহার করতে পারেন।
import { LocalNotifications } from '@capacitor/local-notifications';
async function scheduleOngoingNotification() {
await LocalNotifications.schedule({
notifications: [
{
title: "Ongoing Notification",
body: "This notification will remain ongoing until you interact with it",
id: 2,
schedule: { at: new Date(new Date().getTime() + 1000 * 5) }, // 5 seconds later
ongoing: true,
sound: null,
}
]
});
}
- ongoing: যদি আপনি নোটিফিকেশনকে ongoing (অথবা স্থায়ী) করতে চান, তবে
ongoing: trueসেট করতে হবে।
৫. Notifying with a Custom Sound
আপনি কাস্টম সাউন্ডও ব্যবহার করতে পারেন নোটিফিকেশনগুলিতে। শুধুমাত্র আপনাকে একটি সাউন্ড ফাইল যুক্ত করতে হবে, যেমন beep.wav, এবং এটি আপনার নোটিফিকেশনের সাথে যুক্ত করা হবে।
import { LocalNotifications } from '@capacitor/local-notifications';
async function scheduleCustomSoundNotification() {
await LocalNotifications.schedule({
notifications: [
{
title: 'Notification with Sound',
body: 'This is a test notification with custom sound',
id: 3,
schedule: { at: new Date(new Date().getTime() + 1000 * 5) },
sound: 'beep.wav'
}
]
});
}
৬. Permission Handling
অ্যাপ্লিকেশন শুরু করার আগে, notification permission চাওয়ার জন্য আপনি এই কোডটি ব্যবহার করতে পারেন:
import { LocalNotifications } from '@capacitor/local-notifications';
async function requestPermission() {
const permission = await LocalNotifications.requestPermission();
if (permission.granted) {
console.log('Permission granted');
} else {
console.log('Permission denied');
}
}
উপসংহার
Local Notifications ব্যবহারের মাধ্যমে আপনার Ionic অ্যাপে ইউজারদেরকে তাদের অ্যাপের সাথে সম্পর্কিত গুরুত্বপূর্ণ তথ্য জানানোর জন্য সাহায্য করতে পারে। এটি মূলত অ্যাপ্লিকেশনের ভিতরে কার্যক্রমের জন্য যেমন রিমাইন্ডার, বিজ্ঞপ্তি, বা ইভেন্ট সম্পর্কিত নোটিফিকেশন পাঠানোর জন্য ব্যবহৃত হয়।
- Capacitor Local Notifications প্লাগইন সেটআপ করার মাধ্যমে আপনি মোবাইল অ্যাপে সহজেই নোটিফিকেশন ব্যবস্থা কার্যকর করতে পারবেন।
- Permission handling এবং notification scheduling এর মাধ্যমে আপনি সঠিক সময়ে ইউজারদের অবহিত করতে পারবেন।
- কাস্টম সাউন্ড, ongoing notifications, এবং action-handling-এর মাধ্যমে আপনার নোটিফিকেশনসকে আরো ইন্টারঅ্যাক্টিভ ও ব্যবহারকারী-বান্ধব করতে পারবেন।
Read more